<?php 
function colores($color) {
    $colores = array(
        array('5b92d6','8fbaef'), 
        array('d6d65b','DFDC62'), 
        array('51ad56','8fbaef'), 
        array('db6f70','db6f70'), 
        array('5e71a0','8fbaef'),
        array('d68e5b','efb68f'),
        array('92989f','bbbec3'));
    
    foreach ($colores as $c) {
        $selected = "";
        if ($c[0] == $color)
            $selected = "selected";
        echo "<option {$selected} value=\"{$c[0]}\" >#{$c[0]}</option>\n";
    }
}
?>

<legend>Registrar Sucursal</legend>
<?php if (isset($this->mensaje["success"])) : ?>
    <?php $mensaje = $this->mensaje["success"][0] ?>
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <?php echo $mensaje->message ?>
    </div>
<?php endif; ?>
<?php if (isset($this->mensaje["error"])) : ?>
    <?php $mensaje = $this->mensaje["error"][0] ?>
    <div class="alert alert-error alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <?php echo $mensaje->message ?>
    </div>
<?php endif; ?>

<form name="form_registrar_sucursal" method="POST" action="" >
    <input type="hidden" name="id" value="<?php echo $this->sucursal->getId() ?>" />
<div class="row-fluid">
    <div class="well span12">
        <div class="span3">
            <div class="control-group">
                <label class="control-label" for="inputDescripcion">Nombre</label>
                <div class="controls">
                    <input id="inputDescripcion" name="descripcion" value="<?php echo $this->sucursal->getDescripcion() ?>" type="text" class="input-large" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputDireccion">Dirección</label>
                <div class="controls">
                    <input id="inputDireccion" name="direccion" value="<?php echo $this->sucursal->getDireccion() ?>" type="text" class="input-xlarge" />
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputDireccion">Telefonos</label>
                <div class="controls">
                    <input id="inputTelefonos" name="telefonos" value="<?php echo $this->sucursal->getTelefonos() ?>" type="text" class="input-xlarge" />
                </div>
            </div>
        </div>
        <div class="span3">
            <div >
                <label class="control-label">Color</label>
                <select name="color" id="colour" class="input-mini">
                    <?php colores($this->sucursal->getColor()); ?>
                </select>
            </div>
            <script type="text/javascript">
                $(document).ready(function(){
                    $('#colour').colourPicker({
                        ico: '<?php echo $this->baseUrl("/js/jquery.colourPicker/jquery.colourPicker.gif") ?>', 
                        title: 'Seleccione un color.'
                    });
                });
            </script>
            
            <div class="control-group">
                <label class="control-label" for="inputPerfil">Negocio</label>
                <div class="controls">
                    <select id="negocio" name="negocio" class="input-mini">
                        <option value="0" <?php echo  ($this->sucursal->getNegocio()==0) ? "selected" : ""; ?>>NO</option>
                        <option value="1" <?php  echo ($this->sucursal->getNegocio()==1) ? "selected" : ""; ?>>SI</option>
                    </select>
                </div>
            </div>
            
            <label class="control-label" for="inputPerfil">Activar</label>
            <input type="hidden" name="activar" value="1" id="btn-activar" />
            <div class="btn-group" data-toggle="buttons-radio">
                <button id="activar_1" data-toggle="button" type="button" value="1" class="btn btn-info <?php echo $this->sucursal->getActivo() == 1 ? "active" : "" ?>">Si</button>
                <button id="activar_2" data-toggle="button" type="button" value="0" class="btn btn-info <?php echo $this->sucursal->getActivo() == 0 ? "active" : "" ?>">No</button>
            </div>
            <script type="text/javascript">
                var btns = ['activar_1', 'activar_2'];
                var input = document.getElementById('btn-activar');
                for(var i = 0; i < btns.length; i++) {
                    document.getElementById(btns[i]).addEventListener('click', function() {
                        input.value = this.value;
                    });
                }
            </script>
        </div>
    </div>
    <div class="form-actions">
        <?php if ($this->sucursal->getId()) : ?>
            <button type="submit" class="btn btn-warning" name="modificar">Modificar</button>
            <button type="submit" class="btn btn-danger" name="eliminar">Eliminar</button>
            <a href="<?php echo $this->baseUrl("admin/usuario/sucursal/") ?>" class="btn btn-primary">Cancelar</a>
        <?php else : ?>
            <button type="submit" class="btn btn-success" name="guardar">Guardar</button>
        <?php endif; ?>
    </div>
</div>
</form>

<div class="row-fluid">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th style="width:60px;">#</th>
                    <th>Nombre</th>
                    <th>Dirección</th>
                    <th>Usuarios</th>
                    <th style="width:50px;"></th>
                </tr>
            </thead>
            <?php if (count($this->sucursales)) : ?>
            <tbody>
                <?php foreach ($this->sucursales as $sucursal) : ?>
                <tr>
                    <td style="text-align:center;"><?php echo $sucursal->getId() ?></td>
                    <td><?php echo $sucursal->getDescripcion() ?></td>
                    <td><?php echo $sucursal->getDireccion() ?></td>
                    <td></td>
                    <td style="background-color: <?php echo "#".$sucursal->getColor(); ?>;">
                        <a href="<?php echo $this->baseUrl("admin/usuario/sucursal/id/".$sucursal->getId()) ?>" class="btn btn-primary">Ver</a>
                    </td>
                </tr>
            </tbody>
            <?php endforeach; ?>
            <?php else : ?>
            <tbody>
                <tr>
                    <td class="5">Sin registros</td>
                </tr>
            </tbody>
            <?php endif; ?>
        </table>
</div>